﻿' This software is licensed under Microsoft Public License (Ms-PL), which governs use of the accompanying software. 
' If you use the software, you accept this license. 

' The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, 
' guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot 
' change. To the extent permitted under your local laws, the contributors exclude the implied warranties of 
' merchantability, fitness for a particular purpose and non-infringement.

Imports System.ServiceModel.Description
Imports System.ServiceModel.Dispatcher

Namespace Description
    ''' <summary>
    ''' Enables the REST programming model for a Windows Communication Foundation (WCF) service.
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class RestHttpBehavior
        Inherits WebHttpBehavior

#Region "Constructor"
        ''' <summary>
        ''' Returns a new instance of the RestHttpBehavior class.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            MyBase.New()
        End Sub
#End Region

#Region "Public Methods"
        ''' <summary>
        ''' Installs a REST server error handler.
        ''' </summary>
        ''' <param name="endpoint"></param>
        ''' <param name="endpointDispatcher"></param>
        ''' <remarks></remarks>
        Protected Overrides Sub AddServerErrorHandlers(endpoint As System.ServiceModel.Description.ServiceEndpoint, endpointDispatcher As Dispatcher.EndpointDispatcher)
            ' install the error handler and setup events
            Dim errorHandler As RestErrorHandler = New RestErrorHandler()
            AddHandler errorHandler.OnHandleError, AddressOf ErrorCapture

            endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(errorHandler)
        End Sub
#End Region

#Region "Protected Methods"
        ''' <summary>
        ''' Used to capture errors from the OnHandleError event of the RestErrorHandler class.
        ''' </summary>
        ''' <param name="e">Exception at hand.</param>
        ''' <remarks></remarks>
        Private Sub ErrorCapture(ByVal e As Exception)
            RaiseEvent OnErrorCapture(e)
        End Sub
#End Region
#Region "Events"
        ''' <summary>
        ''' Fired when unhandled exceptions are raised.
        ''' </summary>
        ''' <param name="e">Exception of error.</param>
        ''' <remarks></remarks>
        Protected Friend Event OnErrorCapture(ByVal e As Exception)
#End Region
    End Class
End Namespace
